2020-2021 Sunseeker Telemetry and Lighting System
timer_a_ex3_upModeInterrupt.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 /*******************************************************************************
33  * MSP430i2xx Timer_A - Up Mode Interrupt
34  *
35  * Description: In this example, the 16 bit Timer_A is configured in up mode and
36  * setup to interrupt once the timer hits the value defined by the user. The
37  * timer uses SMCLK divided down to 256kHz as its clock source. This example is
38  * useful if you are trying to perform a periodic task, and can easily be
39  * adapted by changing the timer period and clock dividers. Once the timer is
40  * equal to the value in CCR0 an interrupt occurs signaling that the desired
41  * time has past. CCR0 does not share an interrupt vector so all interrupt flag
42  * checking and clearing is performed by hardware. In the given example the
43  * LED should toggle every 250ms. The period calculation can be seen below.
44  *
45  * MSP430i2041
46  * ------------------
47  * /|\| |
48  * | | |
49  * --|RST P1.4|--->LED
50  * | |
51  * | |
52  * | |
53  *
54  *
55  * Author: Zack Lalanne
56  ******************************************************************************/
57 #include "driverlib.h"
58 
59 // 250ms Period Calculation:
60 // SMCLK = DCO / 8 = 2.048MHz
61 // TACLK = SMCLK / 8 = 256kHz
62 // 250ms / (1 / 256kHz) = 64000
63 #define TIMER_PERIOD 64000
64 
65 int main(void) {
66 
67  Timer_A_initUpModeParam upModeConfig = {
68  TIMER_A_CLOCKSOURCE_SMCLK, // SMCLK Clock Source
69  TIMER_A_CLOCKSOURCE_DIVIDER_8, // SMCLK/8 = 256kHz
70  TIMER_PERIOD, // 250ms
71  TIMER_A_TAIE_INTERRUPT_DISABLE, // Disable Overflow ISR
72  TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE, // Enable CCR0 interrupt
73  TIMER_A_DO_CLEAR // Clear Counter
74  };
75 
76  WDT_hold(WDT_BASE);
77 
78  // Setting P1.4 as LED Pin
79  GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN4);
80  GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN4);
81 
82  // Setting the DCO to use the internal resistor. DCO will be at 16.384MHz
83  // SMCLK = DCO / 8 = 2.048MHz
84  CS_setupDCO(CS_INTERNAL_RESISTOR);
85  CS_initClockSignal(CS_SMCLK, CS_CLOCK_DIVIDER_8);
86 
87  // Configure the timer to use ACLK and interrupt when timer reaches CCR0
88  Timer_A_initUpMode(TIMER_A0_BASE, &upModeConfig);
89 
90  Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);
91 
92  // Go into LPM0 with interrupts enabled
93  __bis_SR_register(LPM0_bits | GIE);
94 }
95 
96 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
97 #pragma vector=TIMER0_A0_VECTOR
98 __interrupt
99 #elif defined(__GNUC__)
100 __attribute__((interrupt(TIMER0_A0_VECTOR)))
101 #endif
102 void TA0_ISR(void) {
103  // TIMER0_A0_VECTOR only contains CCR0 interrupt
104  // No need to check/clear interrupt flags, toggle LED to show ISR reached
105  GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN4);
106 }
GPIO_setOutputLowOnPin(GPIO_PORT_LED1|GPIO_PORT_LED2, GPIO_PIN_LED1|GPIO_PIN_LED2)
void TA0_ISR(void)
int main(void)
#define TIMER_PERIOD